<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:cusdepartgroup:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:cusdepartgroup:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:cusdepartgroup:export']"
        >导出</el-button>
      </el-col> -->
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-tabs type="border-card">
      <el-tab-pane label="客户经理">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户经理" prop="seller">
        <!-- <el-select v-model="queryParams.sellerid" placeholder="请选择客户经理" filterable clearable >
          <el-option
            v-for="item in sellers"
            :key="item.guid"
            :label="item.realname"
            :value="item.guid"
          />
        </el-select> -->
        <el-input
          v-model="queryParams.seller"
          placeholder="请输入客户经理"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户" prop="customername">
        <!-- <el-select v-model="queryParams.customerid" placeholder="请选择客户" filterable clearable >
          <el-option
            v-for="item in customers"
            :key="item.guid"
            :label="item.name"
            :value="item.guid"
          />
        </el-select> -->
        <el-input
          v-model="queryParams.customername"
          placeholder="请输入客户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="省份" prop="province">
        <el-input
          v-model="queryParams.province"
          placeholder="请输入省份"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="城市" prop="city">
        <el-input
          v-model="queryParams.city"
          placeholder="请输入城市"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable >
          <el-option
            label="正常"
            value="1"
          />
           <el-option
            label="删除"
            value="2"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>



    <el-table v-loading="loading" :data="cusdepartgroupList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户经理" align="center" prop="seller" width="160" >
        <template slot-scope="scope">
          <span>{{scope.row.seller + '-' + scope.row.sellerPhone}}</span>
        </template>
      </el-table-column>
      <el-table-column label="客户名称" align="center" prop="customername" />
      <el-table-column label="联系人" align="center" prop="contact" width="60px" />
      <el-table-column label="联系电话" align="center" prop="contactNo" />
      <el-table-column label="地址" align="center" width="240">
        <template slot-scope="scope">
          <span>{{scope.row.province + '' + scope.row.city + '' + scope.row.district + '' + scope.row.address}}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="operator" width="60px" />
      <el-table-column label="创建时间" align="center" prop="createtime" width="80px"></el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="60px">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 1">正常</span>
          <span v-if="scope.row.status == 2">删除</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['system:cusdepartgroup:remove']"-->
<!--          >删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

      </el-tab-pane>
      <el-tab-pane label="渠道客户">
        <el-form :model="queryParamsDe" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="渠道" prop="departid">
        <treeselect style="width: 297px;" v-model="queryParamsDe.departid" :options="deptOptions" :show-count="true" placeholder="请选择部门" />
<!--        <el-select v-model="queryParamsDe.departid" placeholder="请选择渠道" filterable clearable >-->
<!--          <el-option-->
<!--            v-for="item in departOptions"-->
<!--            :key="item.guid"-->
<!--            :label="item.name"-->
<!--            :value="item.guid"-->
<!--          />-->
<!--        </el-select>-->
      </el-form-item>
      <el-form-item label="客户" prop="customerid">
        <!-- <el-select v-model="queryParamsDe.customerid" placeholder="请选择客户" filterable clearable >
          <el-option
            v-for="item in customers"
            :key="item.guid"
            :label="item.name"
            :value="item.guid"
          />
        </el-select> -->
        <el-input
          v-model="queryParamsDe.customername"
          placeholder="请输入客户"
          clearable
          size="mini"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParamsDe.status" placeholder="请选择状态" clearable >
          <el-option
            label="正常"
            value="1"
          />
           <el-option
            label="删除"
            value="2"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQueryDe">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="cusdepartgroupListDe" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="渠道" align="center" prop="departname" />
      <el-table-column label="客户" align="center" prop="customername" />
      <!-- <el-table-column label="商务人员" align="center" prop="seller" />
      <el-table-column label="商务人员电话" align="center" prop="sellerPhone" /> -->
      <el-table-column label="创建人" align="center" prop="operator" />
      <el-table-column label="创建时间" align="center" prop="createtime" width="180"></el-table-column>
       <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 1">正常</span>
          <span v-if="scope.row.status == 2">删除</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:cusdepartgroup:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="totalDe>0"
      :total="totalDe"
      :page.sync="queryParamsDe.pageNum"
      :limit.sync="queryParamsDe.pageSize"
      @pagination="getDeList"
    />
      </el-tab-pane>
    </el-tabs>

    <!-- 添加或修改部门客户关联对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="客户" prop="customerid">
          <el-select v-model="form.customerid" placeholder="请输入并选择客户" remote :remote-method="getCustomers" :loading="remoteLoading" filterable clearable >
            <el-option
              v-for="item in customers"
              :key="item.guid"
              :label="item.name"
              :value="item.guid"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="管理类型" prop="type">
        <el-select v-model="form.type" placeholder="请选择管理类型" filterable clearable @clear="cle">
          <el-option key=1 value=1 label="渠道" @click.native="getRoom($event, 1)"/>
          <el-option key=2 value=2 label="客户经理" @click.native="getRoom($event, 2)"/>
        </el-select>
      </el-form-item>
        <el-form-item label="客户经理" prop="sellerid" v-if="sellShow">
          <el-select v-model="form.sellerid" placeholder="请输入用户姓名模糊匹配" filterable clearable  remote :remote-method="getSellers" :loading="remoteLoading">
            <el-option
              v-for="item in sellers"
              :key="item.guid"
              :label="item.realname"
              :value="item.guid"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="部门" prop="departid" v-if="departShow">
          <el-select v-model="form.departid" placeholder="请选择部门" filterable clearable >
            <el-option
              v-for="item in departOptions"
              :key="item.guid"
              :label="item.name"
              :value="item.guid"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { treeselect } from "@/api/system/dept";
import { listCusDepartGroup, getCusDepartGroup, delCusDepartGroup, addCusDepartGroup, updateCusDepartGroup, exportCusDepartGroup } from "@/api/customer/cusdepartgroup";
import { tuserList,getCustomerList,getDepartmentList } from "@/api/public";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "CusDepartGroup",
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      totalDe: 0,
      // 部门客户关联表格数据
      cusdepartgroupList: [],
      cusdepartgroupListDe: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerid: null,
        sellerid: null,
        type: 2,
        status: "1",
        seller: null,
        customername: null,
        province: null,
        city: null,
      },
      queryParamsDe: {
        pageNum: 1,
        pageSize: 10,
        customerid: null,
        departid: null,
        type: 1,
        status: "1",
        customername: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        customerid: [
          { required: true, message: "客户不能为空", trigger: "change" }
        ],
        type: [
          { required: true, message: "管理类型不能为空", trigger: "change" }
        ],
        sellerid: [
          { required: true, message: "客户经理不能为空", trigger: "change" }
        ],
        departid: [
          { required: true, message: "部门不能为空", trigger: "change" }
        ],
      },
      customers: [],
      sellers: [],
      departOptions: [],
      sellShow: false,
      departShow: false,
      deptOptions: [],
      remoteLoading: false
    };
  },
  created() {
    this.getList();
    this.getDeList();
    this.getDepartmentList();
    this.getTreeselect();
  },
  methods: {
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    getRoom(event, type){
      console.log(type);
      if(type == 1){
        this.departShow = true;
        this.sellShow = false;
      }else{
        this.departShow = false;
        this.sellShow = true;
      }
    },
    cle(){
      console.log("清空")
      this.sellShow = false;
      this.departShow = false;
    },
    /** 查询部门客户关联列表 */
    getList() {
      this.loading = true;
      listCusDepartGroup(this.queryParams).then(response => {
        this.cusdepartgroupList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getDeList() {
      this.loading = true;
      listCusDepartGroup(this.queryParamsDe).then(response => {
        this.cusdepartgroupListDe = response.rows;
        this.totalDe = response.total;
        this.loading = false;
      });
    },
    getDepartmentList(){
      getDepartmentList().then(response => {
        this.departOptions = response.data;
      });
    },
    getCustomers(query = '') {
      this.remoteLoading = true;
      getCustomerList({name: query, status:1}).then(response => {
        this.remoteLoading = false;
        this.customers = response.data;
      });
    },
    getSellers(query = '') {
      this.remoteLoading = true;
      tuserList({realname:query}).then(response => {
        this.remoteLoading = false;
        this.sellers = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.sellShow = false;
      this.departShow = false;
      this.form = {
        guid: null,
        departid: null,
        customerid: null,
        status: 1,
        createtime: null,
        operatorid: null,
        lastupdate: null,
        sellerid: null,
        type: null,
        operator: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
     handleQueryDe() {
      this.queryParamsDe.pageNum = 1;
      this.getDeList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        customerid: null,
        sellerid: null,
        type: 2,
        status: "1",
        seller: null,
        customername: null,
        province: null,
        city: null,
      },
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.guid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
       // this.getCustomers();
      // this.getSellers();
      this.title = "新增客户分配";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const guid = row.guid || this.ids
      getCusDepartGroup(guid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户分配";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.guid != null) {
            updateCusDepartGroup(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.getDeList();
            });
          } else {
            addCusDepartGroup(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.getDeList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const guids = row.guid || this.ids;
      this.$confirm('是否确认删除该数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCusDepartGroup(guids);
        }).then(() => {
          this.getList();
          this.getDeList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有部门客户关联数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCusDepartGroup(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
